package com.emser.unc.client.presenter;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.event.MenuEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class MenuPresenter  implements Presenter{
	private final Display display;
	private SimpleEventBus eventBus;
	
	public interface Display {	
		HasClickHandlers getBtnSellers();
		HasClickHandlers getBtnCustomers();
		HasClickHandlers getBtnDevices();
		HasClickHandlers getBtnProducts();
		HasClickHandlers getBtnReports();
		void repaintBtn(ClickEvent event);
	    Widget asWidget(); 
	}
	
	public MenuPresenter( SimpleEventBus eventBus, Display display) {
		this.eventBus =eventBus;
		this.display = display;
		bind();
	}
		
	private void bind() {
		this.display.getBtnSellers().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  handlerSellerEvent(event);
		      }
		});
		
		this.display.getBtnCustomers().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  handlerCustomersEvent(event);
		      }
		});
		
		this.display.getBtnDevices().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  handlerEquiposEvent(event);
		      }
		});
		
		this.display.getBtnProducts().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  handlerProductsEvent(event);
			      }
		});
		
		this.display.getBtnReports().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  handlerReportsEvent(event);
		      }
		});
	}
	
	private void handlerReportsEvent(ClickEvent e) {
		this.display.repaintBtn(e);
		this.eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuReports()));
	}

	private void handlerProductsEvent(ClickEvent e) {
		this.display.repaintBtn(e);
		this.eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuProducts()));
	}

	private void handlerEquiposEvent(ClickEvent e) {
		this.display.repaintBtn(e);
		this.eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuDevices()));
	}

	private void handlerCustomersEvent(ClickEvent e) {
		this.display.repaintBtn(e);
		this.eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuCustomers()));
	}

	private void handlerSellerEvent(ClickEvent e) {
		this.display.repaintBtn(e);
		this.eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuSellers()));
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
	    container.add(display.asWidget());
	}
}
